<%--
  Created by IntelliJ IDEA.
  User: 11838
  Date: 2020/11/23
  Time: 22:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<link rel="stylesheet" href="${pageContext.request.contextPath}/style/layui/css/layui.css">
<script src="${pageContext.request.contextPath}/style/layui/layui.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/style/jquery-2.1.0.js"></script>
<html>
<head>
    <title>设施类型</title>
    <script>
        $(function (){
            layui.use(['table','form'], function(){
                var table = layui.table;
                var form=layui.form;
                table.render({
                    elem: '#test' //id为test的table
                    ,url:'${pageContext.request.contextPath}/Factype/facType'   //url 查询数据   所有的客户的数据
                    ,title: '设施类型'
                    ,limits:1
                    ,limit:1
                    //data[{ id:12,name:123}]
                    ,cols: [[
                        {field:'id', title:'ID',fixed: 'left', sort: true,align:'center'}
                        ,{field:'name', title:'设施类型',align:'center'}
                        ,{field:'count', title:'数量',align:'center',hide:true}
                        ,{field:'status', title:'状态',templet:function (data){
                                if (data.status==0){
                                    return "正常";
                                }else {
                                    return "已停用";
                                }
                            }}
                        ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
                    ]]
                    ,page: true
                    ,limit:10
                    , done: function (res, curr, count) {
                        var that = this.elem.next();
                        res.data.forEach(function (item, index) {
                            if (item.status === 1) {
                                var tr = that.find(".layui-table-box tbody tr[data-index='" + index + "']");
                                tr.css("background-color", "gray");//目标单元格颜色
                                tr.css("color", "white");//被选中的单元格字体颜色 ：后勤部
                            }
                        });
                    }, parseData: function (res) {
                        if (res.code == -1) {
                            layuiHelp.AlertMsg(res.msg, res.icon);
                        }
                    }
                });
                form.on('submit(demo2)',function (data){

                    table.reload("test",{
                        page:{
                            curr:1
                        },where:data.field
                    })
                    return false;
                });
                $("#re").click(function (){
                    location=location;
                })



                table.on('tool(test)', function(obj){
                    //obj 代表 一行的内容
                    var data = obj.data; //一行的数据
                    //console.log(obj)
                    if(obj.event === 'del'){
                        alert(data.id);
                        layer.confirm('真的删除了吗', function(index){
                            // obj.del();//页面删除
                            $.ajax({
                                url:"${pageContext.request.contextPath}/Factype/delFacType",
                                data:{id:data.id},
                                success:function(){
                                    layer.closeAll();
                                    table.reload("test");
                                    form.render();
                                },
                                error:function () {
                                    layer.closeAll();
                                    alert("操作失败");
                                }
                            })
                            return false;
                        });
                    } else if(obj.event === 'edit'){
                        layui.use('layer', function(){
                            var layer = layui.layer;
                            layer.open({
                                title:'修改信息'
                                ,type: 1
                                ,content:$('#add')
                                ,offset: '150px'
                                ,moveOut:true
                                ,area: ['450px', '200px']

                            });
                            form.val('add', {
                                "id": data.id // "name": "value"
                                ,"name": data.name
                            });
                        })
                    }else if (obj.event === 'caozuo') {
                        /*confirmTrans: function(){*/
                        //配置一个透明的询问框
                        layer.msg('', {
                            content: $("#caozuo"),
                            closeBtn: 1,
                            time: 10000,
                            cancel: function () {
                                location = location;
                            },
                            success: function () {
                                $("#hid").val(data.id)
                            }
                        });
                    }


                    $("#one").click(function (){
                        var id=$("#hid").val();
                        /* layer.closeAll();*/
                        $.ajax({
                            url:"${pageContext.request.contextPath}/Factype/bad",
                            data:{id:id},
                            async:false,
                            success:function (d){
                                if (d>0){
                                    layer.msg("已停用")
                                }
                            },error:function (){
                                alert(55)
                            }
                        })
                        setTimeout(function() {
                            window.location.reload();
                        },400);
                    })

                    $("#two").click(function (){
                        var id=$("#hid").val();
                        /* layer.closeAll();*/
                        $.ajax({
                            url:"${pageContext.request.contextPath}/Factype/good",
                            data:{id:id},
                            async:false,
                            success:function (d){
                                if (d>0){
                                    layer.msg("已恢复");
                                }
                            },error:function (){
                                alert(66)
                            }
                        })
                        setTimeout(function() {
                            window.location.reload();
                        },400);
                    })
                });
                $("#addbtn").click(function () {
                    $('#addform')[0].reset();
                    layui.use(['layer','form','table'], function(){
                        var layer = layui.layer;
                        var form = layui.form;
                        var table = layui.table;
                        layer.open({
                            title:'添加设施'
                            ,type: 1
                            ,content: $("#add")
                            ,offset: '100px'
                            ,moveOut:true
                            ,area: ['450px', '200px']
                        });
                        form.render('select');
                    })
                })
                form.on('submit(add)',function (data) {
                    $.ajax({
                        url:"${pageContext.request.contextPath}/Factype/addFacType",
                        data:data.field,
                        success:function(){
                            layer.closeAll();
                            table.reload("test");
                            $('#addform')[0].reset();
                            form.render();
                        },
                        error:function () {
                            layer.closeAll();
                            alert("添加失败");
                        }
                    })
                    return false;
                })
            })
        })

    </script>
</head>
<body>
<%--    查询--%>
<div id="min">
    <form class="layui-form" action="" id="adataform" lay-filter="ggg">
        <div class="layui-inline">
            <label class="layui-form-label" style="width: 10px">id</label>
            <div class="layui-input-inline">
                <input type="text"  name="id" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-inline">
            <label class="layui-form-label">设施类型</label>
            <div class="layui-input-inline">
                <input type="text"  name="name" autocomplete="off" class="layui-input">
            </div>
        </div>


        <div class="layui-input-inline">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo2">查询</button>
            <button type="reset" class="layui-btn layui-btn-primary" id="re">重置</button>
        </div>

    </form>
</div>



    <button type="button" class="layui-btn layui-btn-sm" id="addbtn">
        <i class="layui-icon">&#xe608;</i> 添加
    </button>
    <%--表格--%>
    <table class="layui-hide" id="test" lay-filter="test"></table>
    <%--命令行事件--%>
    <script type="text/html" id="barDemo">
        <button type="button" class="layui-btn layui-btn-sm layui-btn-normal" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i>
        </button>
        <%--<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>--%>
        {{# if(d.count>0) { }}
        <button  class="layui-btn layui-btn-disabled layui-btn-xs">操作</button>
        {{# }else{ }}
        <button lay-event="caozuo" class="layui-btn layui-btn-warm layui-btn-xs">操作</button>
        {{# } }}
    </script>

    <div class="layui-row" id="add" style="display:none;">
        <div class="layui-col-md10">
            <form class="layui-form layui-from-pane" id="addform" lay-filter="add" style="margin-top:20px" >

                <div class="layui-form-item" style="display: none">
                    <label class="layui-form-label">id</label>
                    <div class="layui-input-block">
                        <input type="text"  name="id"  required  autocomplete="off"  class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">设施类型</label>
                    <div class="layui-input-block">
                        <input type="text"  name="name"  required  lay-verify="required" autocomplete="off"  class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item" style="margin-top:40px">
                    <div class="layui-input-block">
                        <button class="layui-btn  layui-btn-submit " lay-submit="" lay-filter="add">确认添加</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div id="caozuo" style="display: none">
        <input type="hidden" id="hid">
        <button id="one" class="layui-btn layui-btn-warm layui-btn-radius">停用</button>
        <button id="two" class="layui-btn layui-btn-danger layui-btn-radius" >恢复</button>
    </div>
</body>
</html>
